package gptma.fo.converter;

import gptma.bo.model.project.Project;
import gptma.fo.controler.Application;
import gptma.fo.utils.JSFUtils;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 * Bug with RI, converter-for-class & selectManyListBox
 * http://fisheye5.cenqua.com
 * /browse/~raw,r=1.1/javaserverfaces-sources/jsf-ri/systest
 * -per-webapp/converter-property-editor/web/selectmanyListbox.jsp does not seem
 * to pass
 * 
 * @author xcl
 */
@FacesConverter("projectConverter")
public class ProjectConverter implements Converter {

	@Override
	@SuppressWarnings("unused")
	public Object getAsObject(FacesContext ctx, UIComponent comp,
			String projectName) {
		Project ret = null;

		if (projectName != null && !projectName.equals("null")) {
			// Check in JNDI maybe
			Application app = (Application) JSFUtils
					.getJsfBean(Application.NAME);
			ret = app.getProject(projectName);
		}

		return ret;
	}

	@Override
	@SuppressWarnings("unused")
	public String getAsString(FacesContext ctx, UIComponent comp, Object project) {
		String ret = null;

		if (project instanceof Project) {
			Project proj = (Project) project;
			ret = proj.getName();
		}

		return ret;
	}

}
